package reflection;

import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class ReflectionUtils {
    public static void main(String[] args) {

    }

    @Test
    public void api01() throws ClassNotFoundException {
        Class<?> personClass = Class.forName("reflection.Person");
        //getName获取全类名
        System.out.println(personClass.getName());
        //getSimpleName获取简单类名
        System.out.println(personClass.getSimpleName());
        //getFields获取本类即父类中public修饰的属性
        Field[] fields = personClass.getFields();
        for (Field field :fields) {
            System.out.println("本类即父类的public属性:" + field.getName());
        }
        //getDeclaredFields获取本类的所有属性
        Field[] declaredFields = personClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println("本类中所有属性:" + declaredField.getName());
        }
        //getMethods获取本类即父类中的public方法
        Method[] methods = personClass.getMethods();
        for (Method method : methods) {
            System.out.println(method.getName());
        }
        //getDeclaredMethods获取本类的所有方法
        Method[] declaredMethods = personClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod.getName());
        }

        //getConstructors获取本类public修饰的构造器
        //getDeclaredConstructors获取本类的所有构造器

        //getSuperclass:以class的形式返回父类的信息
        Class<?> superclass = personClass.getSuperclass();
        System.out.println(superclass);

    }

    @Test
    public void api02() throws ClassNotFoundException {
        Class<?> personClass = Class.forName("reflection.Person");
        //getModifiers
        Field[] declaredFields = personClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println("本类中所有属性:" + declaredField.getName() +
                    "该属性的修饰符的值:" + declaredField.getModifiers());
        }
    }


}

class A{
    public String hobby;

    public void f1(){}
}

class Person extends A{
    public String name;
    protected int age;
    String job;
    protected double sal;

    public void m1(){

    }

    protected void m2(){

    }

    void m3(){

    }

    private void m4(){

    }


}
